package com.jinlejia.erp.ui.mine.client;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;

import androidx.databinding.DataBindingUtil;

import com.jinlejia.erp.R;
import com.jinlejia.erp.base.BaseActivity;
import com.jinlejia.erp.bean.Clients;
import com.jinlejia.erp.databinding.ActivityClientListBinding;
import com.jinlejia.erp.ui.common.CallAndMessageDialog;
import com.jinlejia.erp.ui.common.ProgressDialog;
import com.jinlejia.erp.ui.mine.adapter.SortAdapter;
import com.jinlejia.erp.utils.CallUtils;
import com.jinlejia.erp.utils.ContactsUtils;
import com.jinlejia.erp.utils.DbUtil;
import com.jinlejia.erp.utils.PinyinComparator;
import com.jinlejia.erp.widget.SideBar;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


/**
 * 通讯录（Activity）
 */

public class ClientListActivity extends BaseActivity {
    /**
     * 汉字转换成拼音的类
     */
//    private CharacterParser characterParser;
    /**
     * 根据拼音来排列ListView里面的数据类
     */
    private PinyinComparator pinyinComparator;

    private SortAdapter adapter;
    private List<Clients> SourceDateList = new ArrayList<>();
    private String phoneNO;
    private CallAndMessageDialog dialog;
    private ProgressDialog proDialog;

    ActivityClientListBinding binding;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_client_list);
        binding.setEvent(this);
        initView();
        initEvent();
    }

    /**
     * 初始化视图
     */
    private void initView() {
        proDialog = new ProgressDialog(this, "请稍后...");
        binding.idListView.setEmptyView(binding.idEmpty);//设置空布局
        SourceDateList = DbUtil.getDB(this).query(Clients.class);
        pinyinComparator = new PinyinComparator();
        // 根据a-z进行排序源数据
        Collections.sort(SourceDateList, pinyinComparator);
        adapter = new SortAdapter(ClientListActivity.this, SourceDateList);

        binding.idListView.setAdapter(adapter);

        getData();

    }

    /**
     * 初始化事件
     */
    private void initEvent() {
        //设置右侧触摸监听
        binding.idSideBar.setOnTouchingLetterChangedListener(new SideBar.OnTouchingLetterChangedListener() {
            @Override
            public void onTouchingLetterChanged(String s) {
                //该字母首次出现的位置
                if (adapter != null) {
                    int position = adapter.getPositionForSection(s.charAt(0));
                    if (position != -1) {
                        binding.idListView.setSelection(position);
                    }
                }
            }
        });
        //根据输入框输入值的改变来过滤搜索
        binding.clientEdit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (TextUtils.isEmpty(binding.clientEdit.getText().toString())) {
                    adapter = new SortAdapter(ClientListActivity.this, SourceDateList);
                    binding.idListView.setAdapter(adapter);
                } else {
                    filterData(binding.clientEdit.getText().toString());
                }

            }
        });


        binding.idListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, final int position, long l) {
                Clients clients = (Clients) adapter.getItem(position);
                //这里要利用adapter.getItem(position)来获取当前position所对应的对象
                phoneNO = clients.getTel();
                dialog = new CallAndMessageDialog(ClientListActivity.this, clients.getName(), phoneNO);
                dialog.setOnCallListener(new CallAndMessageDialog.OnCallListener() {
                    @Override
                    public void onCall() {
                        CallUtils.CallTel(ClientListActivity.this, phoneNO);
                    }

                    @Override
                    public void onMessage() {

                    }
                });
                dialog.show();
            }
        });

    }


    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back_btn:
                finish();
                break;
            case R.id.add_btn:
                //startActivity(new Intent(ClientListActivity.this, GroupsActivity.class));
                break;
            case R.id.client_close:
                binding.clientEdit.setText("");
                break;
        }
    }

    /**
     * 根据输入框中的值来过滤数据并更新ListView
     *
     * @param filterStr
     */
    private void filterData(String filterStr) {
        List<Clients> filterDateList = new ArrayList<>();

        if (TextUtils.isEmpty(filterStr)) {
            filterDateList = SourceDateList;
        } else {
            filterDateList.clear();
            for (Clients clients : SourceDateList) {
                String name = clients.getName();
                String deptName = clients.getDeptName();
                String phone = clients.getTel();

                if (!TextUtils.isEmpty(phone) && phone.indexOf(filterStr.toString()) != -1) {
                    filterDateList.add(clients);
                }
                if (name.indexOf(filterStr.toString()) != -1 || clients.getPin().toUpperCase().indexOf(filterStr.toString().toUpperCase()) != -1) {
                    filterDateList.add(clients);
                    continue;
                }
                if (!TextUtils.isEmpty(deptName)) {
                    if (deptName.indexOf(filterStr.toString()) != -1 || clients.getSpell().toUpperCase().indexOf(filterStr.toString().toUpperCase()) != -1) {
                        filterDateList.add(clients);
                    }
                }
            }
        }
        if (adapter != null) {
            // 根据a-z进行排序
            Collections.sort(filterDateList, pinyinComparator);
            adapter.updateListView(filterDateList, filterStr);
        }
    }

    /**
     * 获取数据
     */
    public void getData() {

        if (SourceDateList.isEmpty()) {
            proDialog.show();
        }

        SourceDateList = ContactsUtils.getInstance(ClientListActivity.this).getContactList();
        // 根据a-z进行排序源数据
        Collections.sort(SourceDateList, pinyinComparator);
        adapter = new SortAdapter(ClientListActivity.this, SourceDateList);
        binding.idListView.setAdapter(adapter);
        proDialog.dismiss();
    }


}
